#include "GameScene.h"
#include "MenuLayer.h"

//------------------------GPauseLayer---------------------------

bool  GPauseLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());
		CCSpriteBatchNode* batchNode =CCSpriteBatchNode::create("ui_atlas.png");

		CCMenuItemSprite *pPauseItem = CCMenuItemSprite::create(
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(31, 133, 28, 29)),
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(31, 133, 28 ,29)),
			this,
			menu_selector(GPauseLayer::menuPauseCallback));

		CCMenuItemSprite *pRestartItem = CCMenuItemSprite::create(
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(51, 92 ,32, 32)),
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(51, 92 ,32 ,32)),
			this,
			menu_selector(GPauseLayer::menuRestartCallback));

		CCSize size =CCDirector::sharedDirector()->getWinSize();
		CCSize itemSize =pPauseItem->getContentSize();
		// Place the menu item bottom-right conner.
		pPauseItem->setPosition(ccp(size.width -itemSize.width/2 , size.height - itemSize.height));
		pRestartItem->setPosition(ccp(size.width-itemSize.width*2,size.height - itemSize.height ));

		// Create a menu with the "close" menu item, it's an auto release object.
		CCMenu* pMenu = CCMenu::create(pPauseItem,pRestartItem, NULL);
		pMenu->setPosition(CCPointZero);
		CC_BREAK_IF(! pMenu);

		this->addChild(pMenu, 1);


		CCSprite *head =CCSprite::createWithTexture(batchNode->getTexture(),CCRect(129 ,47 ,300 ,42));
		head->setAnchorPoint(ccp(0.5,1));
		head->setPosition(ccp(size.width /2, size.height));
		this->addChild(head, -9999);

		bRet = true;
	} while (0);

	return bRet;
}

void GPauseLayer::menuPauseCallback(CCObject* pSender)
{
	CCLog("Node");
	MenuLayer* layer =MenuLayer::create();
	CCNode* p =	this->getParent();

	p->addChild(layer,9999);
	cocos2d::CCDirector::sharedDirector()->pause();
	/*GameLayer *layer =(GameLayer*)(this->getParent()->getChildByTag(1));
	layer ->changeSpeed(!doubleSpeed);*/
}
void GPauseLayer::menuRestartCallback(CCObject* pSender)
{
	GameLayer *layer =GameLayer::sharedGameLayer();
	layer->restart();
}
